/*
 * Copyright (c) 2023 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include "awm_m11p_card.h"
#include "awm_m11p_gpu.h"
#include "awm_m11p_memory.h"
#include "devmon/driver_abi.h"
#include "mc/log.h"
#include <iostream>
#include <memory>
#ifndef DEVICE_DRIVER_NAME
#define DEVICE_DRIVER_NAME "awm_m11p"
#endif

using namespace dev;

// 导出信息实现
extern "C" {

driver_handle_t create_awm_m11p_card(void* service, const char* name) {
    try {
        if (service == nullptr) {
            return nullptr;
        }
        awm_m11p_card* device = new awm_m11p_card();
        device->set_service(static_cast<mc::engine::service*>(service));
        device->set_object_name(name);
        return (void*)device;
    } catch (const std::exception& e) {
        elog("Error creating PCIeGpuCard: ${error}", ("error", e.what()));
        return nullptr;
    }
}
status_t init_awm_m11p_card(driver_handle_t device, void* csr_object, void* connector) {
    awm_m11p_card*    device_object  = static_cast<awm_m11p_card*>(device);
    mc::mutable_dict* csr_object_ptr = static_cast<mc::mutable_dict*>(csr_object);
    mc::dict*         connector_ptr  = static_cast<mc::dict*>(connector);
    bool              ret            = device_object->init(*csr_object_ptr, *connector_ptr);
    if (!ret) {
        return STATUS_ERROR;
    }
    return STATUS_OK;
}

status_t start_awm_m11p_card(driver_handle_t device) {
    awm_m11p_card* device_object = static_cast<awm_m11p_card*>(device);
    device_object->start();
    return STATUS_OK;
}

status_t stop_awm_m11p_card(driver_handle_t device) {
    awm_m11p_card* device_object = static_cast<awm_m11p_card*>(device);
    device_object->stop();
    return STATUS_OK;
}

driver_handle_t create_awm_m11p_gpu(void* service, const char* name) {
    try {
        if (service == nullptr) {
            return nullptr;
        }
        awm_m11p_gpu* device = new awm_m11p_gpu();
        device->set_service(static_cast<mc::engine::service*>(service));
        device->set_object_name(name);
        return (void*)device;
    } catch (const std::exception& e) {
        elog("Error creating NetworkPort: ${error}", ("error", e.what()));
        return nullptr;
    }
}
status_t init_awm_m11p_gpu(driver_handle_t device, void* csr_object, void* connector) {
    awm_m11p_gpu*     device_object  = static_cast<awm_m11p_gpu*>(device);
    mc::mutable_dict* csr_object_ptr = static_cast<mc::mutable_dict*>(csr_object);
    mc::dict*         connector_ptr  = static_cast<mc::dict*>(connector);
    bool              ret            = device_object->init(*csr_object_ptr, *connector_ptr);
    if (!ret) {
        return STATUS_ERROR;
    }

    return STATUS_OK;
}

status_t start_awm_m11p_gpu(driver_handle_t device) {
    awm_m11p_gpu* device_object = static_cast<awm_m11p_gpu*>(device);
    device_object->start();
    return STATUS_OK;
}

status_t stop_awm_m11p_gpu(driver_handle_t device) {
    awm_m11p_gpu* device_object = static_cast<awm_m11p_gpu*>(device);
    device_object->stop();
    return STATUS_OK;
}

driver_handle_t create_awm_m11p_memory(void* service, const char* name) {
    try {
        if (service == nullptr) {
            return nullptr;
        }
        awm_m11p_memory* device = new awm_m11p_memory();
        device->set_service(static_cast<mc::engine::service*>(service));
        device->set_object_name(name);
        return (void*)device;
    } catch (const std::exception& e) {
        elog("Error creating NetworkPort: ${error}", ("error", e.what()));
        return nullptr;
    }
}
status_t init_awm_m11p_memory(driver_handle_t device, void* csr_object, void* connector) {
    awm_m11p_memory*  device_object  = static_cast<awm_m11p_memory*>(device);
    mc::mutable_dict* csr_object_ptr = static_cast<mc::mutable_dict*>(csr_object);
    mc::dict*         connector_ptr  = static_cast<mc::dict*>(connector);
    bool              ret            = device_object->init(*csr_object_ptr, *connector_ptr);
    if (!ret) {
        return STATUS_ERROR;
    }

    return STATUS_OK;
}

status_t start_awm_m11p_memory(driver_handle_t device) {
    awm_m11p_memory* device_object = static_cast<awm_m11p_memory*>(device);
    device_object->start();
    return STATUS_OK;
}

status_t stop_awm_m11p_memory(driver_handle_t device) {
    awm_m11p_memory* device_object = static_cast<awm_m11p_memory*>(device);
    device_object->stop();
    return STATUS_OK;
}

// pcie_gpu_card设备类信息
device_driver_t awm_m11p_card_device_driver = {.device_name = "PCIeGpuCard",
                                               .ctor        = create_awm_m11p_card,
                                               .init        = init_awm_m11p_card,
                                               .start       = start_awm_m11p_card,
                                               .stop        = stop_awm_m11p_card};

// gpu设备类信息
device_driver_t awm_m11p_gpu_device_driver = {.device_name = "GraphicsController",
                                              .ctor        = create_awm_m11p_gpu,
                                              .init        = init_awm_m11p_gpu,
                                              .start       = start_awm_m11p_gpu,
                                              .stop        = stop_awm_m11p_gpu};

// memory设备类信息
device_driver_t awm_m11p_memory_device_driver = {.device_name = "Memory",
                                                 .ctor        = create_awm_m11p_memory,
                                                 .init        = init_awm_m11p_memory,
                                                 .start       = start_awm_m11p_memory,
                                                 .stop        = stop_awm_m11p_memory};

// 导出设备类信息
device_driver_t awm_m11p_driver[] = {awm_m11p_card_device_driver, awm_m11p_gpu_device_driver,
                                     awm_m11p_memory_device_driver};
status_t        register_device_driver(device_driver_t** device_driver, uint8_t* count) {
    *device_driver = awm_m11p_driver;
    *count         = sizeof(awm_m11p_driver) / sizeof(awm_m11p_driver[0]);
    return STATUS_OK;
}
}